package com.example.opengldemo.skeleton;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import android.opengl.GLES30;


public class LoadedObjectVertexNormalTexture
{	
	int texId;
	
	int mProgram;
    int muMVPMatrixHandle;
    int muMMatrixHandle;
    int maPositionHandle;
    int maNormalHandle;
    int maLightLocationHandle;
    int maCameraHandle;
    int maTexCoorHandle;
    String mVertexShader;
    String mFragmentShader;
	
	FloatBuffer   mVertexBuffer;
	FloatBuffer   mNormalBuffer;
	FloatBuffer   mTexCoorBuffer;
    int vCount=0;  
    
    public LoadedObjectVertexNormalTexture(MySurfaceView mv,float[] vertices,float[] normals,float texCoors[],int texId)
    {    	
    	this.texId=texId;

    	initVertexData(vertices,normals,texCoors);

    	initShader(mv);
    }
    

    public void initVertexData(float[] vertices,float[] normals,float texCoors[])
    {

    	vCount=vertices.length/3;   
		


        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
        vbb.order(ByteOrder.nativeOrder());
        mVertexBuffer = vbb.asFloatBuffer();
        mVertexBuffer.put(vertices);
        mVertexBuffer.position(0);
        

        ByteBuffer cbb = ByteBuffer.allocateDirect(normals.length*4);
        cbb.order(ByteOrder.nativeOrder());
        mNormalBuffer = cbb.asFloatBuffer();
        mNormalBuffer.put(normals);
        mNormalBuffer.position(0);

        ByteBuffer tbb = ByteBuffer.allocateDirect(texCoors.length*4);
        tbb.order(ByteOrder.nativeOrder());
        mTexCoorBuffer = tbb.asFloatBuffer();
        mTexCoorBuffer.put(texCoors);
        mTexCoorBuffer.position(0);
    }

     public void initShader(MySurfaceView mv)
    {

        mVertexShader=ShaderUtil.loadFromAssetsFile("skeleton/vertex.sh", mv.getResources());

        mFragmentShader=ShaderUtil.loadFromAssetsFile("skeleton/frag.sh", mv.getResources());

        mProgram = ShaderUtil.createProgram(mVertexShader, mFragmentShader);

        maPositionHandle = GLES30.glGetAttribLocation(mProgram, "aPosition");

        maNormalHandle= GLES30.glGetAttribLocation(mProgram, "aNormal");

        muMVPMatrixHandle = GLES30.glGetUniformLocation(mProgram, "uMVPMatrix");  

        muMMatrixHandle = GLES30.glGetUniformLocation(mProgram, "uMMatrix"); 

        maLightLocationHandle=GLES30.glGetUniformLocation(mProgram, "uLightLocation");

        maTexCoorHandle= GLES30.glGetAttribLocation(mProgram, "aTexCoor"); 

        maCameraHandle=GLES30.glGetUniformLocation(mProgram, "uCamera"); 
    }
    
    public void drawSelf()
    {        
    	 
    	 //�ƶ�ʹ��ĳ����ɫ������
    	 GLES30.glUseProgram(mProgram);    	 
         //�����ձ任��������ɫ������
         GLES30.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);         
         //��λ�á���ת�任��������ɫ������
         GLES30.glUniformMatrix4fv(muMMatrixHandle, 1, false, MatrixState.getMMatrix(), 0); 
         //����Դλ�ô�����ɫ������   
         GLES30.glUniform3fv(maLightLocationHandle, 1, MatrixState.lightPositionFB);
         //�������λ�ô�����ɫ������   
         GLES30.glUniform3fv(maCameraHandle, 1, MatrixState.cameraFB);
         
         // ������λ�����ݴ�����Ⱦ����
         GLES30.glVertexAttribPointer  
         (
         		maPositionHandle,   
         		3, 
         		GLES30.GL_FLOAT, 
         		false,
                3*4,   
                mVertexBuffer
         );       
         //�����㷨�������ݴ�����Ⱦ����
         GLES30.glVertexAttribPointer  
         (
        		maNormalHandle, 
         		3,   
         		GLES30.GL_FLOAT, 
         		false,
                3*4,   
                mNormalBuffer
         );   
         //Ϊ����ָ������������������
         GLES30.glVertexAttribPointer  
         (
        		maTexCoorHandle, 
         		2, 
         		GLES30.GL_FLOAT, 
         		false,
                2*4,   
                mTexCoorBuffer
         );
         //���ö���λ�á���������������������
         GLES30.glEnableVertexAttribArray(maPositionHandle);  
         GLES30.glEnableVertexAttribArray(maNormalHandle);  
         GLES30.glEnableVertexAttribArray(maTexCoorHandle); 
         //������
         GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
         GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, texId);
         //���Ƽ��ص�����
         GLES30.glDrawArrays(GLES30.GL_TRIANGLES, 0, vCount); 
    }
}
